<template>
  <div class="loginin">
    <div v-if="state == 1">
      <div class="psend">
        <el-input
          placeholder="手机号"
          style="width: 389px"
          v-model="user.phone"
        >
          <template slot="prepend"
            ><img
              src="@/assets/img/phonelogin.png"
              style="width: 30px; height: 30px"
              alt=""
          /></template>
        </el-input>
        <input
          type="button"
          @click="sendcode"
          id="validateCodeButton"
          value="发送验证码"
        />
      </div>
      <div style="margin-top: 30px">
        <el-input
          placeholder="验证码"
          type="text"
          style="width: 500px"
          v-model="vcode"
        >
          <template slot="prepend"
            ><img
              src="@/assets/img/vcode.png"
              style="width: 30px; height: 30px"
              alt=""
          /></template>
        </el-input>
      </div>
      <div class="loginbt">
        <el-button type="primary" style="width: 500px" @click="plogin"
          >登录</el-button
        >
      </div>
      <div class="labt">
        <a href="#" @click="pswlogin">密码登录</a>
        <a href="#" @click="fopsw">忘记密码</a>
      </div>
    </div>
    <!-- <component :is="startName"></component> -->
  </div>
</template>

<script>
import axios from 'axios'
import Header from '../Header.vue'

export default {
  data() {
    return {
      state: 1,
      vcode: '',
      user: {
        phone: '',
      },
    }
  },
  methods: {
    //点击登录
    async plogin() {
      const that = this
      const { data: res } = await axios({
        method: 'POST',
        url: 'http://localhost:81/user/login?vcode='+ that.vcode,
        data:that.user
      })
      console.log('手机登录返回数据', res)
      if (res.code == 200) {
        localStorage.setItem('token',res.data)
        // that.state = 0
        // that.$emit('loginsta', that.state)
        that.$router.replace('/')
      } else {
        that.$message.error(res.msg)
      }
    },
    //基于定时器实现30秒倒计时效果
    setTime(targetElement) {
      //倒计时初始化秒数
      var countdown = 30
      var id = setInterval(setCountdown, 1000, targetElement)
      //设置倒计时
      function setCountdown(targetElement) {
        if (countdown == 0) {
          targetElement.removeAttribute('disabled')
          targetElement.value = '重新获取验证码'
          countdown = 30
          clearInterval(id) //停止调用函数
        } else {
          targetElement.setAttribute('disabled', true)
          targetElement.value = '重新发送(' + countdown + ')'
          countdown--
        }
      }
    },
    pswlogin() {
      const that = this
      that.$emit('Registdata', 'Loginin')
    },
    //发送验证码
    async sendcode() {
      const that = this
      var phone = that.user.phone
      var reg = /^[1][3,4,5,7,8][0-9]{9}$/
      var flag = reg.test(phone)
      if (!flag) {
        //不通过
        that.$message.error('请输入正确手机号')
        return null
      }
      const { data: res } = await axios.get(
        'http://localhost:81/checkCode/sengloginmsg?phone=' + that.user.phone
      )
      if (res.code == 200) {
        console.log('发送成功')
        //倒计时限制秒数
        var validateCodeButton = document.getElementById('validateCodeButton')
        that.setTime(validateCodeButton)
      } else {
        that.$message.error(res.msg)
      }
    },
    //点击忘记密码
    fopsw(){
      const that = this
      that.$emit('Registdata','Forgitpsw')
    }
  },
  mounted() {},
  components: {
    Header,
  },
}
</script>

<style lang="less" scoped>
.loginin {
  display: flex;
  position: absolute;
  top: 330px;
  right: 350px;
  width: 500px;
  height: 400px;
}
#codeimg {
  width: 90px;
  height: 35px;
  position: absolute;
  border-radius: 5px;
  margin-left: 5px;
}
.loginmid {
  display: flex;
  position: absolute;
  top: 160px;
  right: 416px;
  input {
    margin-top: 25px;
  }
}
.loginbt {
  display: flex;
  position: absolute;
  top: 180px;
  right: 0;
}
.labt {
  display: flex;
  width: 100%;
  position: absolute;
  top: 260px;
  justify-content: space-between;
}
.psend {
  display: flex;
}
</style>
